package com.unbank.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.unbank.ES.ESClient;
import com.unbank.entity.CrawlDataWithHeat;
import com.unbank.entity.FetchData;
import com.unbank.entity.News;
import com.unbank.entity.SearchData;
import com.unbank.net.Fetcher;
import com.unbank.search.SearchClient;
import com.unbank.search.SearchErrorInfo;
import com.unbank.values.SysParameters;

@Component
@Path("/search")
public class SearchService {

	@Path("/searchByTag")
	@GET
	public String searchByTag(@QueryParam("tagName") String tagName, @QueryParam("keywords") String keywords,
			@QueryParam("websiteID") String websiteID, @QueryParam("pageSize") String pageSize,
			@QueryParam("from") String from) {
		SearchClient searchClient = new SearchClient();
		
		Integer size = 20;
		Integer f = 0;
		
		try {
			if (tagName == null) {
				return SearchErrorInfo.ILLEGALARGS;
			}
			if (pageSize != null) {
				size = Integer.valueOf(pageSize);
			}
			if (from != null) {
				f = Integer.valueOf(from);
			}
			if(websiteID!=null){
				 Integer.valueOf(websiteID);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			// 参数错误
			return SearchErrorInfo.ILLEGALARGS;
		}
		String Neo4JData = "";
		Fetcher fetcher = new Fetcher();
		Map<String, String> map = new HashMap<String, String>();
		map.put("tag", tagName);
		map.put("begin", String.valueOf(f));
		map.put("num", String.valueOf(size));
		if (keywords != null) {
			map.put("keyWords", keywords);
		}
		if (websiteID != null) {
			map.put("webSiteId", websiteID);
			Neo4JData = fetcher.fetch(map, SysParameters.SEARCHBYTAGANDWEBSITEIDURL);
		} else {
			Neo4JData = fetcher.fetch(map, SysParameters.SEARCHBYTAGURL);
		}
		Gson gson = new Gson();
		FetchData fd = gson.fromJson(Neo4JData, FetchData.class);
		List<Integer> list = new ArrayList<Integer>();
		
		for (CrawlDataWithHeat cdwh : fd.getData()) {
			list.add(cdwh.getDocId());
		}
		
		ESClient es=new ESClient();
		List<News> newsList= es.SearchByCrawlIdsOrderByCrawlTime(list, f, size);
		SearchData sd=new SearchData();
		sd.setCount(fd.getCount());
		sd.setData(newsList);
		Gson gson2 = new Gson();
		return gson2.toJson(sd);
	}

	@Path("/searchByWord")
	@GET
	public String searchByWord(@QueryParam("word") String word, @QueryParam("pageSize") String pageSize,
			@QueryParam("from") String from) {
		SearchClient searchClient = new SearchClient();
		
		Integer size = 20;
		Integer f = 0;
		
		try {
			if (pageSize != null) {
				size = Integer.valueOf(pageSize);
			}
			if (from != null) {
				f = Integer.valueOf(from);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			// 参数错误
			return SearchErrorInfo.ILLEGALARGS;
		}
		
		List<News> newsList = searchClient.SearchByWord(word, f, size);
		Gson gson = new Gson();
		return gson.toJson(newsList);
	}

	@Path("/searchByNewsId/{newsId}/")
	@GET
	public String searchByCrawlId(@PathParam("newsId") String newsId) {
		SearchClient searchClient = new SearchClient();
		
		if (newsId == null) {
			// 参数错误
			return SearchErrorInfo.ILLEGALARGS;
		}
		int crawl_id = Integer.valueOf(newsId);

		News news = searchClient.SearchByCrawlId(crawl_id);
		Gson gson = new Gson();
		return gson.toJson(news);
	}

	
}
